package com.nifostasky.comic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nifostasky.comic.entitys.SysMenu;
import com.nifostasky.comic.exceptions.BusinessException;
import com.nifostasky.comic.service.SysMenuService;
import com.nifostasky.comic.mapper.SysMenuMapper;
import com.nifostasky.comic.query.SysMenuQuery;
import com.nifostasky.comic.utils.PageUtils;
import com.nifostasky.comic.vos.ResultCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Override
    public IPage<SysMenu> findListPage(PageUtils pageUtils, SysMenuQuery menuQuery) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(menuQuery.getMenuName()),SysMenu::getMenuName,menuQuery.getMenuName());
        queryWrapper.eq(Objects.nonNull(menuQuery.getMenuType()),SysMenu::getMenuType,menuQuery.getMenuType());
        queryWrapper.eq(Objects.nonNull(menuQuery.getState()),SysMenu::getState,menuQuery.getState());
        IPage<SysMenu> page = this.baseMapper.selectPage(new Page<>(pageUtils.getPageIndex(),pageUtils.getPageSize()),queryWrapper);
        return page;
    }

    @Override
    public Boolean updateMenuState(Long id, Integer state) {
        if(Objects.isNull(id) || Objects.isNull(state)){
            throw  new BusinessException("缺少必填参数");
        }
       int result = this.baseMapper.updateMenuState(id,state);
        Boolean bool = false;
        if(result>0){
            bool = true;
        }
        return bool;
    }

    @Override
    public Boolean saveOrUpdateMenu(SysMenu sysMenu) {
        //验证code是否唯一
        SysMenu menu = this.getSysMenuByMenuMode(sysMenu.getMenuCode());
        if (Objects.nonNull(menu)) {
            if (Objects.isNull(sysMenu.getId())) {
                throw new BusinessException(ResultCode.MENU_CODE_EXIST);
            } else if (Objects.nonNull(sysMenu.getId()) && !Objects.equals(sysMenu.getId(), menu.getId())) {
                throw new BusinessException(ResultCode.MENU_CODE_EXIST);
            }
        }
        return this.saveOrUpdate(sysMenu);
    }

    @Override
    public SysMenu getSysMenuByMenuMode(String menuCode) {
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getMenuCode,menuCode);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<SysMenu> findDataListByIds(List<Long> listId) {
        if(CollectionUtils.isEmpty(listId)){
            return null;
        }
        LambdaQueryWrapper<SysMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMenu::getState,1);
        queryWrapper.in(SysMenu::getId,listId);
        return this.list(queryWrapper);
    }
}
